package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.iems.dao.DutyTimeSectionDAO;

/**
 * 工作时间分段统计作业 jinjiayu
 */
public class DutyTimeSectionService {
	
	private DutyTimeSectionDAO dutyTimeSectionDAO;
	
	public DutyTimeSectionDAO getDutyTimeSectionDAO() {
		return dutyTimeSectionDAO;
	}

	public void setDutyTimeSectionDAO(DutyTimeSectionDAO dutyTimeSectionDAO) {
		this.dutyTimeSectionDAO = dutyTimeSectionDAO;
	}
	
	/**
	 * 统计执行
	 */
	public void startExecu(){
		//这次统计时间
		String thisWorkDay = DateUtil.addDay(-1).replace("-", "");
		boolean isExit = dutyTimeSectionDAO.checkCreateRoadInfo(thisWorkDay);
		if(!isExit){
			System.out.println("工作时间分段统计表不存在"+thisWorkDay);
		}
		
		//sql语句集合列表
		List<String> sqlList = new ArrayList<String>();
		
		List<Map<String,Object>> resList = dutyTimeSectionDAO.getDutyTimeRes(thisWorkDay);
		
		Map<String, List<Map<String, Object>>> valCntListMap = new ListUtil().groupBy("vclid", resList);
		
		Map<String,Object> valMap = new HashMap<String, Object>();
		Map<String,Object> mapVal = new HashMap<String, Object>();
		List<Map<String,Object>> valList = null;
		List<Map<String,Object>> resultList = new LinkedList<Map<String,Object>>();
		for (Map.Entry<String, List<Map<String, Object>>> entry : valCntListMap.entrySet()) {
			valList = entry.getValue();
			
			for(int i=0;i<valList.size();i++){
				mapVal = valList.get(i);
				if(0 == i){
					valMap = mapVal;
					resultList.add(valMap);
				}else if(i == valList.size()-1){
					if(MapGetter.getString(valMap, "stat").equals(MapGetter.getString(mapVal, "stat"))){
						valMap.put("ltime", MapGetter.getString(mapVal, "ltime"));
						valMap.put("llo", MapGetter.getString(mapVal, "llo"));
						valMap.put("lla", MapGetter.getString(mapVal, "lla"));
					}else{
						resultList.add(mapVal);
					}
				}else{
					if(MapGetter.getString(valMap, "stat").equals(MapGetter.getString(mapVal, "stat"))){
						valMap.put("ltime", MapGetter.getString(mapVal, "ltime"));
						valMap.put("llo", MapGetter.getString(mapVal, "llo"));
						valMap.put("lla", MapGetter.getString(mapVal, "lla"));
					}else{
						resultList.add(mapVal);
						valMap = mapVal;
					}
				}
			}
			
			valMap = new HashMap<String, Object>();
			valList = null;
		}
		
		dutyTimeSectionDAO.insertDutyTimeSection(thisWorkDay,resultList,sqlList);
		
		try {
			this.dutyTimeSectionDAO.executeSql(sqlList);
			//statKeyOnTime();
			sqlList = null;
		} catch (Exception e) {
			System.out.println("工作时间分段统计作业出现异常："+e);
		}
		resList = null;
		valCntListMap = null;
		resultList = null;
	}
	/**
	 * 统计计算开机时长
	 */
	public void statKeyOnTime(){
		//获得昨天的日期字符串，格式yyyyMMdd
		String tableDate = DateUtil.addDay(-1);
		String queryBgDa = tableDate+" 00:00:00";
		String queryEdDa = tableDate+" 23:59:59";
		StringBuffer sb = new StringBuffer();
		sb.append(" insert into Message_Stat_VclKeyOn (Msvk_VclID,Msvk_StatTime,Msvk_KeyOnTime)");
		sb.append(" select MsgSVS_VclID,Convert(NVARCHAR(10),MsgSVS_BgnTime,120) ms,ISNULL(SUM (DATEDIFF(S,MsgSVS_BgnTime,MsgSVS_EndTime)),0) keyOnTime ");
		sb.append(" from Message_Stat_VclState_"+tableDate.replace("-", "")+" left join V_VehicleInfo on MsgSVS_VclID = VehicleID");
		sb.append(" where MsgSVS_BgnTime>='"+queryBgDa+"' and MsgSVS_EndTime<= '"+queryEdDa+"' ");
		sb.append(" and (MsgSVS_State = '1' or MsgSVS_State = '3') group by MsgSVS_VclID,Convert(NVARCHAR(10),MsgSVS_BgnTime,120) ");
		try{
			int i = this.dutyTimeSectionDAO.execInsert(sb.toString(), new Object[]{});
			if(i>0){
				System.out.println("统计计算开机时长,插入表-------------------成功！");
			} else{
				System.out.println("统计计算开机时长,插入表-------------------失败！");
			}
		}catch(Exception e){
			System.out.println("统计计算开机时长,插入表-------------------失败,异常："+e);
		}
	}
}

